package com.tadpole.music.presenter.home;

import android.util.Log;

import com.tadpole.music.bean.home.HomeNewInfoBean;
import com.tadpole.music.callback.BaseCallback;
import com.tadpole.music.config.Constant;
import com.tadpole.music.config.UrlConfig;
import com.tadpole.music.iView.home.HomeNewInfoIView;
import com.tadpole.music.model.base.DataModel;
import com.tadpole.music.model.base.ModelToken;
import com.tadpole.music.presenter.base.BasePresenter;
import com.tadpole.music.utils.JsonParseUtil;
import com.tadpole.music.utils.SpUtil;
import com.tadpole.music.utils.ToastUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页最新资讯
 * Created By 孙岩
 * on 2020/12/5
 */
public class HomeNewInfoPresenter extends BasePresenter<HomeNewInfoIView> {
    @SuppressWarnings("unchecked")
    public void getHomeNewInfo(){
        if(isViewAttached()){
            String url = UrlConfig.getHomeInfoV2;
            String token = SpUtil.getInstance(getView().getContext()).getString(Constant.TOKEN,"");
            DataModel.request(ModelToken.GET_HEAD_REQUEST)
                    .url(url)
                    .token(token)
                    .execute(new BaseCallback<String>() {
                        @Override
                        public void onSuccess(String data) {
                            Log.d("sunyan",data);
                            try {
                                JSONObject obj = new JSONObject(data);
                                if(obj.getInt("code")==200){
                                    JSONArray array = new JSONArray(obj.getString("data"));
                                    List<HomeNewInfoBean.DataBean> list = new ArrayList<>();
                                    for(int i=0;i<array.length();i++){
                                        JSONObject object = new JSONObject(array.get(i).toString());
                                        HomeNewInfoBean.DataBean bean = new HomeNewInfoBean.DataBean(
                                                JsonParseUtil.getStr(object,"desc"),
                                                JsonParseUtil.getInt(object,"id"),
                                                JsonParseUtil.getStr(object,"thumb"),
                                                JsonParseUtil.getStr(object,"title")
                                        );
                                        list.add(bean);
                                    }
                                    getView().showHomeNewInfo(list);
                                }else if(obj.getInt("code")==201){
                                    getView().show201();
                                }else{
                                    ToastUtils.show(obj.getString("msg"));
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            getView().show201();
                            getView().showErr();
                            Log.d("sunyan",msg);
                        }

                        @Override
                        public void onComplete() {

                        }

                        @Override
                        public void onBefore() {

                        }
                    });
        }
    }
}
